package net.sky.oa.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import net.sky.oa.util.ReflectionUtils;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

/**
 * 领导信箱
 * TalBox. @author hond
 */
@Entity
@Table(name = "TAL_BOX")//领导信箱
public class TalBox implements java.io.Serializable {

	private static final long serialVersionUID = 5673991033245515351L;
	
	// Fields

	protected Long id;
	private Integer type;      //建议类型
	private String title;   //信箱标题
	private String content; //信箱内容
	private Date createtime;//提交信箱时间
	private Integer isshow; //发送类型：1实名2匿名
	private Integer status;    //状态
	private SysEmployee user; //发送人
    
	private Set<TalBoxUser> TalBoxUsers;
	// Constructors

	/** default constructor */
	public TalBox() {
	}
	
	@Id
    @Column(name = "ID", unique = true, nullable = false)
	@SequenceGenerator(name="SQ_TALBOX_ID",sequenceName="SQ_TALBOX_ID",allocationSize=1)
	@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SQ_TALBOX_ID")
	public Long getId() {
        return id;
    }

    public void setId(Long id){
    	this.id = id;
    }

	/** minimal constructor */
	public TalBox(Long id) {
		this.id = id;
	}
	
	@Column(name = "TYPE", precision = 2, scale = 0)
	public Integer getType() {
		return this.type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	@Column(name = "TITLE", length = 60)
	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name = "CONTENT")
	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "CREATETIME")
	public Date getCreatetime() {
		return this.createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}

	@Column(name = "ISSHOW", precision = 1, scale = 0)
	public Integer getIsshow() {
		return this.isshow;
	}

	public void setIsshow(Integer isshow) {
		this.isshow = isshow;
	}

	@Column(name = "STATUS", precision = 2, scale = 0)
	public Integer getStatus() {
		return this.status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "USER_ID")
	public SysEmployee getUser() {
		return this.user;
	}

	public void setUser(SysEmployee user) {
		this.user = user;
	}
    
	@OneToMany(fetch=FetchType.EAGER,mappedBy="box")
	@Cascade(value={CascadeType.SAVE_UPDATE,CascadeType.DELETE_ORPHAN}) 
	public Set<TalBoxUser> getTalBoxUsers() {
		return TalBoxUsers;
	}

	public void setTalBoxUsers(Set<TalBoxUser> talBoxUsers) {
		TalBoxUsers = talBoxUsers;
	}
	
	@Transient
	public Set<SysEmployee> getBoxEmployees() {
		Set<SysEmployee> bs=new HashSet<SysEmployee>();
		for(TalBoxUser bu:TalBoxUsers){
			bs.add(bu.getUser());
		}
		return bs;
	}
	
	/**
	 * 作用：
	 * 1、把原来选中现在选中的保留
	 * 2、把原来选中现在没有选中的删除掉
	 * 3、把原来没有选中现在选中的增加进去
	 * @param checkUserFiledIds
	 */
	@Transient
	public void mergeByCheckUserFiledIds(final Collection<Long> checkUserFiledIds) {
		Set<Long> set=new HashSet<Long>();
		List<Long> list=new ArrayList<Long>();
		for(Iterator<Long> iter=checkUserFiledIds.iterator();iter.hasNext();){
			Long id=iter.next();
			if(set.add(id)){
				list.add(id);
			}
		}
		checkUserFiledIds.clear();
		checkUserFiledIds.addAll(list);
		
		Set<TalBoxUser> boxUsers=this.getTalBoxUsers();
		 if(boxUsers==null){
			 boxUsers=new HashSet<TalBoxUser>();
		 }
		//目标集合为空, 删除源集合中所有对象后直接返回.
		 Iterator<TalBoxUser> it=boxUsers.iterator();
		 try {
		    while(it.hasNext()){
		    	TalBoxUser element=it.next();
		    	Long id=element.getUser().getId();
		    	if(!checkUserFiledIds.contains(id)){
		    		it.remove();
		    	}else{
		    		checkUserFiledIds.remove(id);
		    	}
		    }
		  //ID集合目前剩余的id均不在源集合中,创建对象,为id属性赋值并添加到源集合中.
		    for(Long id :checkUserFiledIds){
		    	TalBoxUser talboxuser=new TalBoxUser();
		    	SysEmployee user=new SysEmployee();
		    	user.setId(id);
		    	talboxuser.setBox(this);
		    	talboxuser.setStatus(1);
		    	talboxuser.setIsread(2);
		    	talboxuser.setUser(user);
		    	boxUsers.add(talboxuser);
		    }
		    this.setTalBoxUsers(boxUsers);
		 } catch (Exception e) {
			e.printStackTrace();
			throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
		 }
	}

}